<?php

namespace App\Admin\Controllers;

use App\Models\AddressModel;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class AddressController extends BaseController
{
    protected $title = '地址管理';

    /**
     * grid
     * @return Grid
     */
    protected function grid(): Grid
    {
        $grid = new Grid(new AddressModel);
        $grid->model()->orderByDesc('id')->select(points());
        //$grid->expandFilter();
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('id', __('ID'))->integer();
                $filter->equal('is_default', __('com.defaultAddress'))->select(__('com.whether'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('user_id', __('com.user_id'))->integer();
            });
        });
        $grid->column('id', __('ID'))->sortable();
        $grid->column('contacts', __('com.contacts'));
        $grid->column('mobile', __('com.mobile'));
        $grid->column('gender', __('com.gender'))->using(__('com.genderArr'));
        $grid->column('province', __('com.province'));
        $grid->column('city', __('com.city'));
        $grid->column('district', __('com.district'));
        $grid->column('street', __('com.street'));
        $grid->column('address', __('com.address'));
        $grid->column('house_number', __('com.house_number'));
        $grid->column('x', __('x'));
        $grid->column('y', __('y'));
        $grid->column('is_default', __('com.defaultAddress'))->using(__('com.whether'));
        $grid->column('created_at', __('com.created_at'))->sortable();
        $grid->column('deleted_at', __('com.deleted_at'));
        return $grid;
    }

    /**
     * detail
     * @param int $id
     * @return Show
     */
    protected function detail(int $id): Show
    {
        $show = new Show(AddressModel::findOrFail($id, points()));
        $show->field('id', __('ID'));
        $show->field('contacts', __('com.contacts'));
        $show->field('mobile', __('com.mobile'));
        $show->field('gender', __('com.gender'))->using(__('com.genderArr'));
        $show->field('province', __('com.province'));
        $show->field('city', __('com.city'));
        $show->field('district', __('com.district'));
        $show->field('street', __('com.street'));
        $show->field('address', __('com.address'));
        $show->field('house_number', __('com.house_number'));
        $show->field('location', __('com.location'));
        $show->field('x', __('x'));
        $show->field('y', __('y'));
        $show->field('is_default', __('com.defaultAddress'))->using(__('com.whether'));
        $show->field('created_at', __('com.created_at'));
        $show->field('deleted_at', __('com.deleted_at'));
        return $show;
    }
}
